package com.FrameWork.Synchoronization;


import com.SFA.R;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

public final class SynchConfig {

	private Context content = null;
	private static SynchConfig config = null;
	private static final String _ConfigName = "SynchConfig";

	public static SynchConfig Instance(Context context) {
		if (config == null)
			config = new SynchConfig(context);
		return config;
	}

	private SynchConfig(Context context) {
		this.content = context;
		SharedPreferences preference = content.getSharedPreferences(
				_ConfigName, Activity.MODE_PRIVATE);
		Version = context.getString(R.string.SyncVersion);
		this.TimeOut =  Integer.parseInt(context.getResources().getString(R.string.Timeout));
		this.LastSyncTime = preference.getLong(LAST_SYNC_TIME, 0);
		int model = preference.getInt(SYNC_MODEL, 0);

		SyncModel sm = model == 1 ? SyncModel.UploadDownload : 
			SyncModel.InitialSync;
		this.setSyncModel(sm);

	}

	private final String SYNC_MODEL = "SyncModel";
	private final String LAST_SYNC_TIME = "LastSyncTime";

	public static String ConnectionString;
	private SyncModel _model;

	// / <summary>
	// / 同步模式
	// / </summary>
	public SyncModel getSyncModel() {
		return _model;
	}

	public void setSyncModel(SyncModel model) {
		_model = model;
	}

	// / <summary>
	// / 获取或设置同步超时（秒）
	// / </summary>
	public int TimeOut;

	// / <summary>
	// / 最后一次同步时间
	// / </summary>
	public long LastSyncTime;

	public boolean EnableGzip = true;
	// / <summary>
	// / 获取或者设置同步服务器端口
	// / </summary>
	public String ServerIP;
	
	public String Version;

	// / <summary>
	// / 获取或设置同步服务器端口
	// / </summary>
	public int ServerPort;

	public boolean UseSSL= false;
	


	// / <summary>
	// / 保存配置到SharedPreference
	// / </summary>
	public void Save() throws Exception {
		SharedPreferences preference = content.getSharedPreferences(
				_ConfigName, Activity.MODE_PRIVATE);
		SharedPreferences.Editor editor = preference.edit();
		editor.putLong(LAST_SYNC_TIME, LastSyncTime);
		editor.putInt(SYNC_MODEL, _model == SyncModel.UploadDownload ? 1	:0);
		// 保存配置
		editor.commit();
		editor = null;
		preference = null;
	}

}
